/**
 * @fileOverview
 */

Karamu = Karamu || {};
Karamu.Controllers = Karamu.Controllers || {};

/**
 * Groups are displayed as categories in desktop view; as tabs in dashboard view.
 *
 * @class
 * @requires jQuery, underscore.js, karamu.js
 * @param {Object} context the top-level controller object, e.g karamu
 */
Karamu.Controllers.Groups = function(context){
    this.context = context;

    /**
     * @param {(Object|string)} element a jQuery object or jQuery selector for a single object to render into
     * @param {Object} options
     * @param {string} options.groups
     * @public
     */
    this.render = function(element, options) {
        this.element = $(element);
        this.options = $.extend({
            gadgetSelector: '.gadget',
            dropzoneSelector: '.dropzone',
            groups: [],
        }, options);

        for (var a = 0, b = this.options.groups.length; a < b; a++) {
            this.context.controllers.group.render(this.element, this.options.groups[a]);
        }
    };

    /**
     * @param {Object} e event object
     * @param {Object} e.data the top-level controller object, e.g karamu
     * @public
     */
    this.change = function(e){
        e.data.controllers.groups.push({
            context: e.data,
            data: e.data.models[e.data.view].groups,
            success: function(){
                if (console && console.log) console.log('View "' + this.view + '" changed.');
                $(this.models[this.view]).trigger('change-model');
            },
            view: e.data.view
        });
    };

    /**
     * @param {?Object} args
     * @param {?Function} args.complete
     * @param {?Object} args.context the top-level controller object, e.g karamu; if omitted, the XHR object is used
     * @param {?Function} args.error
     * @param {?Function} args.success
     * @param {string} args.view 'desktop' or 'dashboard'
     * @public
     */
    this.fetch = function(args) {
        $.ajax({
            cache: false,
            complete: args.complete,
            context: args.context,
            dataType: 'json',
            error: args.error,
            success: args.success,
            type: 'GET',
            url: args.context.services.groups.replace('%data%', args.view + '-' + args.context.userId)
        });
    };

    /**
     * @param {?Object} args
     * @param {?Function} args.complete
     * @param {?Object} args.context the top-level controller object, e.g karamu; if omitted, the XHR object is used
     * @param {?Object} args.data
     * @param {?Function} args.error
     * @param {?Function} args.success
     * @param {string} args.view 'desktop' or 'dashboard'
     * @public
     */
    this.push = function(args){
        $.ajax({
            cache: false,
            complete: args.complete,
            contentType: 'application/json',
            context: args.context,
            data: JSON.stringify(args.data),
            error: args.error,
            success: args.success,
            type: 'PUT',
            url: args.context.services.groups.replace('%data%', args.view + '-' + args.context.userId)
        });
    };

    /**
     * @param {?Object} args
     * @param {?Function} args.complete
     * @param {?Object} args.context the top-level controller object, e.g karamu; if omitted, the XHR object is used
     * @param {?Function} args.error
     * @param {?Function} args.success
     * @param {string} args.view 'desktop' or 'dashboard'
     * @public
     */
    this.trash = function(args){
        $.ajax({
            cache: false,
            complete: args.complete,
            context: args.context,
            error: args.error,
            success: args.success,
            type: 'DELETE',
            url: args.context.services.groups.replace('%data%', args.view + '-' + args.context.userId)
        });
    };

};
